
#import "Data.h"
#import "JSONKit.h"

#define kLampDataLamps @"lamps"
#define kLampDataWattLumen @"watt_lumen"
#define kLampDataWattLumenTypeClassic @"classic"
#define kLampDataWattLumenTypeHalogen @"halogen"
#define kLampDataWattLumenTypeReflector12v @"reflector12v"
#define kLampDataWattLumenTypeReflector230v @"reflector230v"


@interface Data ()

@property (nonatomic, readonly) NSDictionary *wattToLumenTables;

@end

@implementation Data

-(id)init
{
    self = [super init];
    if (self) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"lampdata" ofType:@"json"];
        NSString *json = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        NSDictionary *lampData = [json objectFromJSONString];

        _lampList = lampData[kLampDataLamps];
        _wattToLumenTables = lampData[kLampDataWattLumen];
    }
    return self;
}

-(NSDictionary *)wattToLumenTableForLampType:(NSString *)typeString
{
    return self.wattToLumenTables[typeString];
}

-(NSString *)lumenStringFromWattValue:(NSInteger)watt lampType:(NSString *)typeString
{
    NSDictionary *conversionTable = self.wattToLumenTables[typeString];
    NSArray *range = conversionTable[[@(watt) stringValue]];
    NSInteger from = [range[0] integerValue];
    NSInteger to = [range[1] integerValue];

    return from == to ? [@(from) stringValue] : [NSString stringWithFormat:@"%d \u2013 %d", from, to];
}

#pragma mark - Singleton stuff
+(Data *)sharedInstance
{
    static Data *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{ sharedInstance = [[Data alloc] init]; });
    return sharedInstance;
}
@end

